package com.ktwlsoft.archivemanage.config;

import com.ktwlsoft.archivemanage.service.security.CustomUserDetailsService;
import com.ktwlsoft.archivemanage.service.security.JwtAuthenticationEntryPoint;
import com.ktwlsoft.archivemanage.service.security.JwtAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.BeanIds;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
        securedEnabled = true,
        jsr250Enabled = true,
        prePostEnabled = true
)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomUserDetailsService customUserDetailsService;

    @Autowired
    private JwtAuthenticationEntryPoint unauthorizedHandler;

    @Bean
    public JwtAuthenticationFilter jwtAuthenticationFilter() {
        return new JwtAuthenticationFilter();
    }

    @Bean(BeanIds.AUTHENTICATION_MANAGER)
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and()
                .csrf()
                .disable()
                .exceptionHandling()
                .authenticationEntryPoint(unauthorizedHandler)
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers("/auth/login/**",
                        "/verificationCode/getPhoneVerificationCode",
                        "/verificationCode/inspectVerificationCode",
                        "/currencyImg/saveNoData",
                        "/currencyImg/save",
                        "/currencyImg/addImg",
                        "/currencyImg/getFileView/**",
                        "/currencyImg/getFileUrlView",
                        "/currencyImg/downloadFile/{fileName}",
                        "/currencyImg/downloadFileByUrl",
                        "/accountManage/save",
                        "/verificationCode/inspectVerificationCode",
                        "/personalSettings/wxRetrievePasswordByPhone",
                        "/personalSettings/retrievePasswordByPhone",
                        "/study/manageView",
                        "/study/userView",
                        "/study/knowledge/view",
                        "/verificationCode/getVerifyCode",
                        "/export/**",
                        "/sysConfig/appUpdate",
                        "/register/save",
                        "/webSocket/**",
                        "/accountManage/getAllInstructors",
                        "/sms/sendBusinessSms")
                .permitAll()
                .anyRequest().authenticated();
        // Add our custom JWT security filter
        http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        PasswordEncoder passwordEncoder = passwordEncoder();
        auth.userDetailsService(customUserDetailsService).passwordEncoder(passwordEncoder);
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    

    @Override
    public void configure(WebSecurity web) throws Exception {

        web.ignoring().antMatchers("/v2/api-docs",
                "/configuration/ui",
                "/swagger-resources/**",
                "/configuration/security",
                "/swagger-ui.html",
                "/swagger-ui.html",
                "/demo/**",
                "/webjars/**");
    }
}
